package org.yang.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yang.reggie.common.CustomException;
import org.yang.reggie.entity.Category;
import org.yang.reggie.entity.Dish;
import org.yang.reggie.entity.Setmeal;
import org.yang.reggie.mapper.CategoryMapper;
import org.yang.reggie.service.CategoryService;
import org.yang.reggie.service.DishService;
import org.yang.reggie.service.SetmealService;

/**
 * @author 缘友一世
 * date 2023/5/12-23:40
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int countOfDish = dishService.count(dishLambdaQueryWrapper);

        //查询当前菜品是否关联菜品分类，如果关联，删除失败，抛出逻辑异常
        if(countOfDish>0) {
            //已经关联菜品，抛出一个业务异常
            throw new CustomException("当前分类已关联菜品");
        }

        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId,id);
        int countOfSetmeal = setmealService.count(setmealQueryWrapper);
        //查询当前菜品是否关联套餐分类，如果关联，删除失败，抛出逻辑异常
        if(countOfSetmeal>0) {
            //已经关联套餐，抛出一个业务异常
            throw new CustomException("当前分类已关联套餐");
        }
        super.removeById(id);
        //正常删除

    }
}
